﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Event
{
    public partial class Event : CGOBase, IActionable, IModuleListener
    {
        //public event ModuleCommunicationEventHandler ModuleCommunication;
        private const string DEFAULT_CONTROL = "EventList.ascx";                

        #region "Event Handlers"

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (ModuleConfiguration.ModuleControl.SupportsPartialRendering)
                {
                    UpdatePanel upContainer = this.Parent.Parent as UpdatePanel;
                    if (upContainer != null)
                    {
                        upContainer.UpdateMode = UpdatePanelUpdateMode.Always;
                    }
                }
                
                LoadModuleControl();
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// Registers the module actions required for interfacing with the portal framework 
        /// </summary> 
        /// <value></value> 
        /// <returns></returns> 
        /// <remarks></remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile),
                   ModuleActionType.AddContent, "", "add.gif", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit,
                    true, false);
                return Actions;
            }
        }

        #endregion

        #region IModuleListener Members

        public void OnModuleCommunication(object s, ModuleCommunicationEventArgs e)
        {
            if (ModuleConfiguration.ModuleControl.SupportsPartialRendering)
            {
                switch (e.Sender)
                {
                    case "CharityGamesOnline.Modules.Charity.CharityNav":
                        HandleCharityCommunication(e);
                        break;
                    case "CharityGamesOnline.Modules.Event.EventNav":
                        HandleEventCommunicastion(e);
                        break;
                }
            }
        }
        #endregion

        #region Private Function

        private void LoadModuleControl()
        {
            string strControlName = DEFAULT_CONTROL;

            if (string.IsNullOrEmpty(Request.QueryString["mctl"]))
            {
                if (!Null.IsNull(Settings["ViewMode"]))
                {
                    strControlName = (string)Settings["ViewMode"];
                }
            }
            else
            {
                strControlName = Request.QueryString["mctl"];
                if (!strControlName.EndsWith(".ascx"))
                    strControlName += ".ascx";
            }
            
            switch (strControlName.ToLower())
            {
                case "eventlist.ascx":
                case "eventlistsimple.ascx":
                case "eventmonth.ascx":
                case "eventnav.ascx":
                case "eventdetail.ascx":
                case "eventmanagement.ascx":
                    LodaControl(strControlName);
                    break;
            }            
        }

        private void LodaControl(string strControlToLoad)
        {
            PortalModuleBase objModule = LoadControl(strControlToLoad) as PortalModuleBase;
            objModule.ModuleConfiguration = ModuleConfiguration.Clone();
            objModule.ID = System.IO.Path.GetFileNameWithoutExtension(strControlToLoad);
            phMain.Controls.Add(objModule); 
        }

        private void HandleCharityCommunication(ModuleCommunicationEventArgs e)
        {
            switch (e.Type)
            {
                case Charity.CharityNav.COMM_TYPE:
                    //EventList objEventList = FindControl<EventList>(phMain);
                    EventListSimple objEventList = FindControl<EventListSimple>(phMain);
                    if (!Null.IsNull(objEventList))
                    {
                        objEventList.BindData(Int32.Parse((string)e.Value));                        
                    }
                    break;
            }
        }

        private void HandleEventCommunicastion(ModuleCommunicationEventArgs e)
        {
            switch (e.Type)
            {
                case EventNav.COMM_TYPE:
                    EventDetail objEventDetail = FindControl<EventDetail>(phMain);
                    if (!Null.IsNull(objEventDetail))
                    {
                        objEventDetail.BindData(Int32.Parse((string)e.Value));
                    }
                    break;
            }
        }
        #endregion
    }
}